﻿using FangChanCrm;
using Nancy.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Maticsoft.Web
{
    public class XiaoXiTiXing
    {
        public string GetQYAccessToken(string corpid, string corpsecret)
        {
            string getAccessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={0}&corpsecret={1}";
            string accessToken = "";

            string respText = "";

            //获取josn数据
            string url = string.Format(getAccessTokenUrl, corpid, corpsecret);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (Stream resStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(resStream, Encoding.Default);
                respText = reader.ReadToEnd();
                resStream.Close();
            }

            try
            {
                JavaScriptSerializer Jss = new JavaScriptSerializer();
                Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);

                //通过键access_token获取值
                accessToken = respDic["access_token"].ToString();
            }
            catch (Exception ex) { }

            return accessToken;
        }

        /// <summary>
        /// Post数据接口
        /// </summary>
        /// <param name="postUrl">接口地址</param>
        /// <param name="paramData">提交json数据</param>
        /// <param name="dataEncode">编码方式</param>
        /// <returns></returns>
        public static  string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = dataEncode.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return ret;
        }

        /// <summary>
        /// 推送信息
        /// </summary>
        /// <param name="corpid">企业号ID</param>
        /// <param name="corpsecret">管理组密钥</param>
        /// <param name="paramData">提交的数据json</param>
        /// <param name="dataEncode">编码方式</param>
        /// <returns></returns>
        public static  string SendQYMessage(string corpid, string corpsecret, string paramData, Encoding dataEncode)
        {
            string accessToken = WeiXin_Common.Get_access_token();
            string postUrl = string.Format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={0}", accessToken);

            return PostWebRequest(postUrl, paramData, dataEncode);
        }





        public static string SendMessage(string userid,string message,string lujing)
        {
            
            string UserOID = userid;
            string Message = ""+message+"<a href='"+lujing+"'>点击查看</a>";
            string sCorpID = "1000002";
            string corpsecret = "KbQzkn49masS-jKaZzv6NaEFoF_zyM-8f5rXPfjh3mM";
            string responeJsonStr = "{";
            responeJsonStr += "\"touser\": \"" + UserOID + "\",";
            responeJsonStr += "\"msgtype\": \"text\",";
            responeJsonStr += "\"agentid\": \"1000002\",";
            responeJsonStr += "\"text\": {";
            responeJsonStr += "  \"content\": \"" + Message + "\"";
            responeJsonStr += "},";
            responeJsonStr += "\"safe\":\"0\"";
            responeJsonStr += "}";

            string s = SendQYMessage(sCorpID, corpsecret, responeJsonStr, Encoding.UTF8);
            return s;
        }
        public static string SendMessage(string userid, string message)
        {

            string UserOID = userid;
            string Message = "" + message + "！";
            string sCorpID = "1000002";
            string corpsecret = "KbQzkn49masS-jKaZzv6NaEFoF_zyM-8f5rXPfjh3mM";
            string responeJsonStr = "{";
            responeJsonStr += "\"touser\": \"" + UserOID + "\",";
            responeJsonStr += "\"msgtype\": \"text\",";
            responeJsonStr += "\"agentid\": \"1000002\",";
            responeJsonStr += "\"text\": {";
            responeJsonStr += "  \"content\": \"" + Message + "\"";
            responeJsonStr += "},";
            responeJsonStr += "\"safe\":\"0\"";
            responeJsonStr += "}";

            string s = SendQYMessage(sCorpID, corpsecret, responeJsonStr, Encoding.UTF8);
            return s;
        }

        public static string SendMessageYDL(string userid, string message)
        {

            string UserOID = userid;
            string Message = "" + message ;
            string sCorpID = "1000002";
            string corpsecret = "KbQzkn49masS-jKaZzv6NaEFoF_zyM-8f5rXPfjh3mM";
            string responeJsonStr = "{";
            responeJsonStr += "\"touser\": \"" + UserOID + "\",";
            responeJsonStr += "\"msgtype\": \"text\",";
            responeJsonStr += "\"agentid\": \"8\",";
            responeJsonStr += "\"text\": {";
            responeJsonStr += "  \"content\": \"" + Message + "\"";
            responeJsonStr += "},";
            responeJsonStr += "\"safe\":\"0\"";
            responeJsonStr += "}";

            string s = SendQYMessage(sCorpID, corpsecret, responeJsonStr, Encoding.UTF8);
            return s;
        }
    }
}